package com.zx.db.mysql.dbutils;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 单例类获取配置文件，为后面util获取连接准备数据.首次调用是顺带初始化。
 * <p>useage : 
 * <p>eg1:	JdbclInit.getInstance();
 * <p>eg2:	JdbclInit.getInstance("res/jdbcsetting.properties");
 * <p>eg3:	JdbclInit.getInstance("res/jdbcsetting.properties","user","pwd","driver","url");
 * <p>eg4:	JdbclInit.getInstance("username","pwd","com.mysql.jdbc.driver","jdbc:mysql://127.0.0.1:3306/information_schema");
 * 
 * <p>首次调用后，后续只需要JdbclInit.getInstance();即可
 * 
 * @author Ken
 * @version 2016年7月5日 下午9:35:02
 */
public class JdbclInit {

	private static Logger LOG = LoggerFactory.getLogger(JdbclInit.class);

	private String username;

	private String password;

	private String driver;

	private String url;

	//	/**是否已经初始化了*/
	//	private Boolean INIT = false;
	//
	//	/**是否需要重新刷新*/
	//	private Boolean FLUSH = false;

	public static JdbclInit instance = null;

	private JdbclInit() {
	}

	private JdbclInit(String username, String password, String driver, String url) {
		this.username = username;
		this.password = password;
		this.driver = driver;
		this.url = url;
	}

	/**
	 * 初始化设置JDBC的基本参数.默认是claspath下的jdbc.properties文件中配置 了jdbc.user,jdbc.password,jdbc.driver,jdbc.url的属性.
	 */
	public static JdbclInit getInstance() {
		return getInstance("jdbc.properties");//jdbc.properties
	}

	/**
	 * 初始化设置JDBC的基本参数.propertieFilePathName文件中配置 了jdbc.user,jdbc.password,jdbc.driver,jdbc.url的属性.
	 */
	public static JdbclInit getInstance(String propertieFilePathName) {
		return getInstance(propertieFilePathName, "jdbc.user", "jdbc.password", "jdbc.driver", "jdbc.url");
	}

	/**
	 * 初始化设置JDBC的基本参数.propertieFilePathName下的jdbc.properties文件中配置 了userKey,passwordKey,driverKey,urlKey的属性.
	 */
	public static JdbclInit getInstance(String propertieFilePathName, String userKey, String passwordKey,
			String driverKey, String urlKey) {
		if (null == instance) {
			try {
				Configuration config = new PropertiesConfiguration(propertieFilePathName);
				LOG.debug("readproperties is :{}", config);
				return getInstance(config.getString(userKey), config.getString(passwordKey), config.getString(driverKey),
						config.getString(urlKey));
			} catch (Exception e) {
				LOG.error("get propertis error", e);
			}
		}
		return instance;
	}

	/**
	 * 初始化设置JDBC的基本参数.userVal,passwordVal,driverVal,urlVal的属性.
	 */
	public synchronized static JdbclInit getInstance(String userVal, String passwordVal, String driverVal,
			String urlVal) {
		if (null == instance) {
			try {
				LOG.debug("configuration direct by Val ");
				instance = new JdbclInit(userVal, passwordVal, driverVal, urlVal);
				LOG.debug("load db connection ok ");
			} catch (Exception e) {
				LOG.error("set jdbc params error", e);
				instance = null;
			}
		}
		return instance;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
